package com.atguigu.stuty.synchronizedaAchieve;

/**
 * @Author early morning
 * @Create 2025/4/22 18:21
 * Description: 资源类：封装类，高内聚，低耦合
 */
public class MyTicket implements Runnable{
    // 定义总票数
    int ticket = 100; // 100张票
    @Override
    public void run() {
        // while 循环
        while (ticket > 0) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            method();
        }
    }
    // 普通的同步方法加锁实现线程安全问题
    public synchronized void method() {
        if(ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "正在出售第" + ticket + "张票");
            ticket --;
        }
    }
}
